package cj.web.marry.controllers;

import java.util.EnumSet;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.marriage.constants.MarriageRegSetMealEnum;
import cj.web.marriage.constants.MarriageRegStatusEnum;
import cj.web.marriage.constants.MarriageRegTypeEnum;
import cj.web.marriage.constants.RegAddressEnum;
import cj.web.marry.dao.RegDAO;

@Path("reg")
public class RegController {
	@Autowired
	private RegDAO regDAO;  
	
	@Get("regPage")
	public String regPage(Model model, @Param("openId")String openId, @Param("type") String type){
		if (openId !=null && openId.length() > 0 && type != null && type.length() > 0) {
			model.add("AddressEnum", EnumSet.allOf(RegAddressEnum.class));
			model.add("type",type);
			
			//获取幸福码
			String code = regDAO.getCode(openId);
			if(code != null && code.length()>0){
				model.add("code",code);
			}else{
				return "r:fail";
			}			
			
			return "/views/marry/regDetail.vm";
		}
		
		return "r:fail";
	}
	
	@Get("divorce")
	public String divorce(Model model, @Param("openId")String openId){
		if (openId !=null && openId.length() > 0) {
			model.add("AddressEnum", EnumSet.allOf(RegAddressEnum.class));
			model.add("type",MarriageRegTypeEnum.lihunyy.getValue());
			
			//获取幸福码
			String code = regDAO.getCode(openId);
			if(code != null && code.length()>0){
				model.add("code",code);
			}else{
				return "r:fail";
			}			
			
			return "/views/marry/divorce.vm";
		}
		
		return "r:fail";
	}
	
	@Post("submit")
	public String submit(@Param("code")String code, @Param("man")String man, @Param("manCard")String manCard, @Param("woman")String woman, 
			@Param("womanCard")String womanCard, @Param("regTime")String regTime, @Param("setMealId")String setMealId, @Param("link")String link, 
			@Param("type")String type, @Param("addressId")String addressId){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		//分别验证男女是否重复，同时防止双方身份证号写反
		int repeatMan = regDAO.repeatMan(manCard,womanCard,type);
		int repeatWoman = regDAO.repeatWoman(manCard,womanCard,type);
		
		if(repeatMan > 0){
			jo.put("man", man);
			jo.put("manCard", manCard);
			
			jo.put("returnFlag", Constants.returnObj.returnFlag_repeat);
		}
		
		if(repeatWoman > 0){
			jo.put("woman", woman);
			jo.put("womanCard", womanCard);
			
			jo.put("returnFlag", Constants.returnObj.returnFlag_repeat);
		}			
		
		if (jo.get("returnFlag").toString().equals(Constants.returnObj.returnFlag_repeat)) {
			return "@" + jo.toString();
		}
		
		int status = MarriageRegStatusEnum.yuyue.getValue();
		try {
			regDAO.create(code,man,manCard,woman,womanCard,regTime,link,addressId,type,status);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}

		return "@" + jo.toString();
	}
	
	//颁证仪式预约
	@Get("ceremony")
	public String ceremony(Model model, @Param("openId")String openId){
		if (openId !=null && openId.length() > 0) {
			model.add("AddressEnum", EnumSet.allOf(RegAddressEnum.class));
			model.add("MealEnum",EnumSet.allOf(MarriageRegSetMealEnum.class));
			
			//获取幸福码
			String code = regDAO.getCode(openId);
			if(code != null && code.length()>0){
				model.add("code",code);
			}else{
				return "r:fail";
			}			
			
			return "/views/marry/ceremony.vm";
		}
		
		return "r:fail";
	}
	
	//颁证仪式预约提交
	@Post("order")
	public String order(@Param("code")String code, @Param("man")String man, @Param("regTime")String regTime, @Param("setMealId")String setMealId, @Param("link")String link,
			@Param("addressId")String addressId){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		int type = MarriageRegTypeEnum.tesebanzheng.getValue();
		
		//验证姓名电话是否重复
		int repeat = regDAO.repeat(man,link,type);
		
		if(repeat > 0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_repeat);
			return "@" + jo.toString(); 
		}
		
		int status = MarriageRegStatusEnum.yuyue.getValue();
		try {
			regDAO.order(code,man,regTime,link,addressId,type,status,setMealId);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}

		return "@" + jo.toString();
	}
	
	@Get("success")
	public String success(){
		return "/views/marry/success.vm";
	}
	
	@Get("fail")
	public String fail(){
		return "/views/marry/fail.vm";
	}
}